package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContext;
import java.util.Map;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.opensymphony.xwork2.ActionContext;
import com.lb.jwitter.util.ApplicationContextProvider;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuLyLichGiaDinh;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuLyLichGiaDinhService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXADMLoaiQuanHe;
import com.lb.jwitter.service.TblQLDTTXADMLoaiQuanHeService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblQLDTTXAHocVientblQLDTTXAHocVienPhieuLyLichGiaDinhUpdateAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private int hocVienId;

	public void setHocVienId(int hocVienId) {
		this.hocVienId = hocVienId;
	}

	public int getHocVienId() {
		return hocVienId;
	}

	private TblQLDTTXAHocVienService tblQLDTTXAHocVienService;

	private String hoTen;

	public void setHoTen(String hoTen) {
		this.hoTen = hoTen;
	}

	public String getHoTen() {
		return hoTen;
	}

	private int quanHeId;

	public void setQuanHeId(int quanHeId) {
		this.quanHeId = quanHeId;
	}

	public int getQuanHeId() {
		return quanHeId;
	}

	private TblQLDTTXADMLoaiQuanHeService tblQLDTTXADMLoaiQuanHeService;

	private String thuongTru;

	public void setThuongTru(String thuongTru) {
		this.thuongTru = thuongTru;
	}

	public String getThuongTru() {
		return thuongTru;
	}

	private String ghiChu;

	public void setGhiChu(String ghiChu) {
		this.ghiChu = ghiChu;
	}

	public String getGhiChu() {
		return ghiChu;
	}

	private TblQLDTTXAHocVienPhieuLyLichGiaDinh tblQLDTTXAHocVienPhieuLyLichGiaDinh;

	public void setTblQLDTTXAHocVienPhieuLyLichGiaDinh(
			TblQLDTTXAHocVienPhieuLyLichGiaDinh tblQLDTTXAHocVienPhieuLyLichGiaDinh) {
		this.tblQLDTTXAHocVienPhieuLyLichGiaDinh = tblQLDTTXAHocVienPhieuLyLichGiaDinh;
	}

	private TblQLDTTXAHocVienPhieuLyLichGiaDinhService tblQLDTTXAHocVienPhieuLyLichGiaDinhService;

	private List<TblQLDTTXAHocVienPhieuLyLichGiaDinh> tblQLDTTXAHocVienPhieuLyLichGiaDinhs;

	public List<TblQLDTTXAHocVienPhieuLyLichGiaDinh> getTblQLDTTXAHocVienPhieuLyLichGiaDinhs() {
		return tblQLDTTXAHocVienPhieuLyLichGiaDinhs;
	}

	private int preUpdate(Message msg, TblQLDTTXAHocVienPhieuLyLichGiaDinh obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postUpdate(Message msg, TblQLDTTXAHocVienPhieuLyLichGiaDinh obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(),
					"tbl_QLDTTXA_HocVien_PhieuLyLich_GiaDinh", "sua")) {
				return "noprivilege";
			}
			tblQLDTTXAHocVienPhieuLyLichGiaDinhService = (TblQLDTTXAHocVienPhieuLyLichGiaDinhService) context
					.getBean("tbl_QLDTTXA_HocVien_PhieuLyLich_GiaDinhClient");
			tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context
					.getBean("tbl_QLDTTXA_HocVienClient");

			tblQLDTTXADMLoaiQuanHeService = (TblQLDTTXADMLoaiQuanHeService) context
					.getBean("tbl_QLDTTXA_DM_LoaiQuanHeClient");

			msg = new Message();
			tblQLDTTXAHocVienPhieuLyLichGiaDinh = tblQLDTTXAHocVienPhieuLyLichGiaDinhService
					.gettblQLDTTXAHocVienPhieuLyLichGiaDinh(new Long(id));
			tblQLDTTXAHocVienPhieuLyLichGiaDinh
					.setHocVienId(tblQLDTTXAHocVienService
							.gettblQLDTTXAHocVien(new Long(hocVienId)));
			tblQLDTTXAHocVienPhieuLyLichGiaDinh.setHoTen(hoTen);
			tblQLDTTXAHocVienPhieuLyLichGiaDinh
					.setQuanHeId(tblQLDTTXADMLoaiQuanHeService
							.gettblQLDTTXADMLoaiQuanHe(new Long(quanHeId)));
			tblQLDTTXAHocVienPhieuLyLichGiaDinh.setThuongTru(thuongTru);
			tblQLDTTXAHocVienPhieuLyLichGiaDinh.setGhiChu(ghiChu);
			preUpdate(msg, tblQLDTTXAHocVienPhieuLyLichGiaDinh);
			if (msg.isPreMessage_b()) {
				tblQLDTTXAHocVienPhieuLyLichGiaDinhService
						.updatetblQLDTTXAHocVienPhieuLyLichGiaDinh(tblQLDTTXAHocVienPhieuLyLichGiaDinh);
				postUpdate(msg, tblQLDTTXAHocVienPhieuLyLichGiaDinh);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}

			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			ConditionReport con = new ConditionReport();
			con.setCol("hocVienId.id");
			con.setValue1(new Long(hocVienId));
			con.setOperator("eq");
			cons.add(con);
			tblQLDTTXAHocVienPhieuLyLichGiaDinhs = tblQLDTTXAHocVienPhieuLyLichGiaDinhService
					.findtblQLDTTXAHocVienPhieuLyLichGiaDinhByCondition(cons,
							pageSize, page);
			pages = tblQLDTTXAHocVienPhieuLyLichGiaDinhService
					.counttblQLDTTXAHocVienPhieuLyLichGiaDinhByCondition(cons)
					/ pageSize + 1;
			dataString = "";
			dataString += "&id=";
			dataString += "&hocVienId=";
			dataString += "&hoTen=";
			dataString += "&quanHeId=";
			dataString += "&thuongTru=";
			dataString += "&ghiChu=";
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out
					.println("Loi Update tbl_QLDTTXA_HocVien_PhieuLyLich_GiaDinh Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
